#include <iostream>

#include <wfrest/HttpServer.h>
#include <wfrest/json.hpp>
#include <workflow/WFFacilities.h>
#include <workflow/MySQLResult.h>

using namespace wfrest;
using std::cout;
using std::endl;
using std::string;

int main()
{
    HttpServer svr;
    WFFacilities::WaitGroup waitGroup(1);

    //通过HttpServer部署不同的API服务
    svr.GET("/wfrest/hello", [](const HttpReq * req, HttpResp *resp) {
        //wfrest::HttpReq是protocol::HttpRequest的派生类
        printf("%s %s %s\n", req->get_method(),
               req->get_request_uri(),
               req->get_http_version());

        string value = req->header("User-Agent");
        cout << "User-Agent: " << value << endl;

        string body = req->body();
        cout << "body:" << body << endl;

        //wfrest::HttpResp也是protocol::HttpResponse的派生类
        resp->append_output_body("world\n");
        //resp->String("world\n");
    });

    svr.POST("/wfrest/post", [](const HttpReq * req, HttpResp *resp) {
        //wfrest::HttpReq是protocol::HttpRequest的派生类
        printf("%s %s %s\n", req->get_method(),
               req->get_request_uri(),
               req->get_http_version());

        string value = req->header("User-Agent");
        cout << "User-Agent: " << value << endl;

        string body = req->body();
        cout << "\nbody:" << body << endl;

        //wfrest::HttpResp也是protocol::HttpResponse的派生类
        //resp->append_output_body("wfrest post test\n");
        resp->String("wfrest post test\n");
    });

    svr.GET("/wfrest/query", [](const HttpReq * req, HttpResp *resp) {
        //获取所有的查询词
        auto & queryList = req->query_list();
        for(auto & elem : queryList) {
            cout << elem.first << ": " << elem.second << endl;
        }
        //通过key来获取value
        cout << req->query("username") << endl;
    });

    svr.POST("/wfrest/json", [](const HttpReq * req, HttpResp *resp) {
        //直接获取JSON对象
        auto & jsonObj = req->json();
        cout << jsonObj["username"] << ", " << jsonObj["password"] << endl;
    });

    svr.POST("/wfrest/urlencoded", [](const HttpReq * req, HttpResp *resp) {
        //直接获取x-www-urlencoded
        auto & formKV = req->form_kv();
        for(auto & elem : formKV) {
            cout << elem.first << ": " << elem.second << endl;
        }
    });

    svr.POST("/wfrest/formdata", [](const HttpReq * req, HttpResp *resp) {
        //直接获取form-data
        auto & form = req->form();
        for(auto & elem : form) {
            cout << elem.first 
                 << ", " << elem.second.first 
                 << ", " << elem.second.second << endl;
        }
    });

    svr.GET("/wfrest/file", [](const HttpReq * req, HttpResp *resp) {
        //部署静态资源
        /* resp->File("./index.html"); */
        resp->File("/usr/local/nginx/html/postform.html");
    });

    svr.POST("/wfrest/file", [](const HttpReq * req, HttpResp *resp) {
        if(req->content_type() == APPLICATION_URLENCODED) {
            auto & formKV = req->form_kv();
            for(auto & elem : formKV) {
                cout << elem.first << ": " << elem.second << endl;
            }
        }

        resp->headers["Content-Type"] = "text/html";
        resp->File("/usr/local/nginx/html/dir/test.html");
    });

    svr.GET("/wfrest/mysql0", [](const HttpReq * req, HttpResp *resp) {
        std::string url = "mysql://root:1234@localhost";
        std::string sql = "select * from cloudisk.tbl_user_token";
        //将MySQL查询的结果集返回给对端
        resp->MySQL(url, sql);
    });

    svr.GET("/wfrest/mysql1", [](const HttpReq * req, HttpResp *resp) {
        std::string url = "mysql://root:1234@localhost";
        std::string sql = "select * from cloudisk.tbl_user_token";
        //将MySQL查询的结果集返回给对端
        resp->MySQL(url, sql, [resp](Json * json){
            resp->String((*json)["result_set"][0]["rows"][0][1]);
        });
    });

    svr.GET("/wfrest/mysql2", [](const HttpReq * req, HttpResp *resp) {
        std::string url = "mysql://root:1234@localhost";
        std::string sql = "select * from cloudisk.tbl_user_token";
        //将MySQL查询的结果集返回给对端
        resp->MySQL(url, sql, [resp](protocol::MySQLResultCursor* cursor){
            std::vector<std::vector<protocol::MySQLCell>> rows;
            cursor->fetch_all(rows);
            resp->String(rows[0][1].as_string());
        });
    });


    if (svr.track().start(8888) == 0)
    {
        svr.list_routes();
        waitGroup.wait();
        svr.stop();
    } else
    {
        fprintf(stderr, "Cannot start server");
        exit(1);
    }
    return 0;
}
